package com.team7.happycommunity.businessservice.controller;

import com.team7.happycommunity.businessservice.common.CommonResult;
import com.team7.happycommunity.businessservice.entity.BusinessImage;
import com.team7.happycommunity.businessservice.entity.ServiceImg;
import com.team7.happycommunity.businessservice.service.ServiceImgService;
import com.team7.happycommunity.businessservice.util.QiniuUploadUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * (ServiceImg)表控制层
 *
 * @author makejava
 * @since 2020-03-26 21:40:52
 */
@RestController
@RequestMapping("serviceImg")
@Api(tags="服务图片接口")
public class ServiceImgController {
    /**
     * 服务对象
     */
    @Resource
    private ServiceImgService serviceImgService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ServiceImg selectOne(Integer id) {
        return this.serviceImgService.queryById(id);
    }



    /**
     * 图片上传
     * @param imgBase
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "图片上传")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "imgBase",value = "图片",required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    })
    public CommonResult upload(String imgBase,Integer userId){
        byte[] b1 = null;
        BASE64Decoder decoder = new BASE64Decoder();
        try{
            if (imgBase.indexOf("data:image/jpeg;base64,") != -1) {
                b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/jpeg;base64,", ""));
            } else {
                if (imgBase.indexOf("data:image/png;base64,") != -1) {
                    b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/png;base64,", ""));
                } else {
                    b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/jpg;base64,", ""));
                }
            }
            for (int i = 0; i < b1.length; ++i) {
                if (b1[i] < 0) {// 调整异常数据
                    b1[i] += 256;
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        //添加图片url；
        String name = UUID.randomUUID().toString().replace("_", "");
        try {
            String upload = new QiniuUploadUtil().upload(name, b1);
            System.out.println("uploaduploadupload"+upload);
            redisTemplate.opsForHash().put(userId+"",UUID.randomUUID(),upload);
            redisTemplate.expire(userId+"", 10, TimeUnit.MINUTES); //设置超时时间10秒 第三个参数控制时间单位，详情查看TimeUnit

        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.success("1");
    }
}